from typing import *


class Solution:
    def maxFreeTime(
        self, eventTime: int, startTime: List[int], endTime: List[int]
    ) -> int:
        n = len(startTime)
        lmx = [0] * (n + 1)
        for i in range(n):
            lr = 0 if i == 0 else endTime[i - 1]
            lmx[i + 1] = max(startTime[i] - lr, lmx[i])
        rmx = ans = 0
        for i in range(n - 1, -1, -1):
            rl = eventTime if i == n - 1 else startTime[i + 1]
            lr = 0 if i == 0 else endTime[i - 1]
            time = endTime[i] - startTime[i]
            ans = (
                max(ans, rl - lr)
                if time <= max(rmx, lmx[i])
                else max(ans, rl - lr - time)
            )
            rmx = max(rmx, rl - endTime[i])
        return ans
